package mscadm

import (
	"github.com/gin-gonic/gin"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
	"yundian/internel/pkg/utils"
)

// HandleStewardStatisticsShop 购物统计
func HandleStewardStatisticsShop(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.StewardStatisticsReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.LoginUserId = userId
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewGoodsDao().HandleStewardStatisticsShop(param)

	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleStewardStatisticsComplaint 投诉统计
func HandleStewardStatisticsComplaint(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.StewardStatisticsReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.LoginUserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)

	data := dao.NewReportDao().StewardStatisticsComplaint(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleStewardStatisticsMaintain 维修统计
func HandleStewardStatisticsMaintain(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.StewardStatisticsReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.Type = 3
	param.LoginUserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)

	data := dao.NewNotificationDaoDao().StewardStatisticsMaintain(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleStewardStatisticsDaily 日用品统计
func HandleStewardStatisticsDaily(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.StewardStatisticsReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.Type = 2
	param.LoginUserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)

	data := dao.NewNotificationDaoDao().StewardStatisticsDaily(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleStewardStatisticsService 服务统计
func HandleStewardStatisticsService(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.StewardStatisticsReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.LoginUserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)

	prlist := dao.NewReportDao().StewardStatisticsAllNum(param).Unwrap().([]*request.StewardStatisticsRes)
	list := dao.NewNotificationDaoDao().StewardStatisticsService(param).Unwrap().([]*request.StewardStatisticsRes)
	Response(ctx)("", CodeSuccess, append(list, prlist...))(OK)
}

// HandleStewardStatisticsServiceDays 服务按天统计
func HandleStewardStatisticsServiceDays(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.StewardStatisticsReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.LoginUserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	days := utils.CreateDays(param.StartTime, param.EndTime, "2006-01-02", 24)

	if len(days) > 365 {
		Response(ctx)("查询范围不能大于365天", CodeInvalidParam, nil)(OK)
		return
	}

	data := dao.NewNotificationDaoDao().StewardStatisticsServiceDays(param)
	list := data.Unwrap().([]*request.StewardStatisticsDaysRes)
	var nList []*request.StewardStatisticsDaysRes
	for _, day := range days {
		target := &request.StewardStatisticsDaysRes{Day: day, Type: param.Type}
		for _, item := range list {
			if day == item.Day {
				target.Num = item.Num
			}
		}
		nList = append(nList, target)
	}

	Response(ctx)("", CodeSuccess, nList)(OK)
}
